local extension = Package:new("tangz_ld_heg")
extension.extensionName = "new_heg_my"
extension.game_modes_whitelist = { "mul_heg_mode" }

local H = require "packages/new_heg_my/util"
local U = require "packages/utility/utility"
-- local D = require "packages/diy_utility/diy_utility"

Fk:loadTranslationTable{
  ["tangz_ld_heg"] = "凌烟绘卷",
}


local lijing = General(extension, "ld__n_lijing", "tangz", 4)
lijing.hidden = true
local talu = fk.CreateTriggerSkill{
  name = "ld__n_talu",
}
lijing:addSkill(talu)
Fk:loadTranslationTable{
  ["ld__n_lijing"] = "李靖",
  ["#ld__n_lijing"] = "",
  ["designer:ld__n_lijing"] = "教父",
  ["illustrator:ld__n_lijing"] = "",

  ["ld__n_talu"] = "踏虏",
  [":ld__n_talu"] = "当你使用黑色【杀】或黑色普通锦囊牌指定唯一目标时，你可以失去1点体力，令位于你与其最短路径上的所有角色成为此牌的额外目标。",

  ["~ld__n_lijing"] = "",
}

local sudingfang = General(extension, "ld__n_sudingfang", "tangz", 4)
sudingfang.hidden = true
local cuixi = fk.CreateTriggerSkill{
  name = "ld__n_cuixi",
}
local changshe = fk.CreateTriggerSkill{
  name = "ld__n_changshe",
}
sudingfang:addSkill(cuixi)
sudingfang:addSkill(changshe)
Fk:loadTranslationTable{
  ["ld__n_sudingfang"] = "苏定方",
  ["#ld__n_sudingfang"] = "",
  ["designer:ld__n_sudingfang"] = "祭祀",
  ["illustrator:ld__n_sudingfang"] = "",

  ["ld__n_cuixi"] = "摧袭",
  [":ld__n_cuixi"] = "出牌阶段限一次，你可以与你攻击范围内你计算距离最大的所有角色依次弃置一张牌，若这些牌牌名各不相同，你对其中一名角色造成1点伤害并结束此阶段。",
  ["ld__n_changshe"] = "长蛇",
  [":ld__n_changshe"] = "阵法技，与你处于同一队列的角色攻击范围+X（X为与你处于同一队列角色数） 。",

  ["~ld__n_sudingfang"] = "",
}

local libai = General(extension, "ld__n_libai", "tangz", 4)
libai.hidden = true
local jiuge = fk.CreateTriggerSkill{
  name = "ld__n_jiuge",
}
local jianxing = fk.CreateTriggerSkill{
  name = "ld__n_jianxing",
}
local shiyin = fk.CreateTriggerSkill{
  name = "ld__n_shiyin",
}
libai:addSkill(jiuge)
libai:addSkill(jianxing)
libai:addSkill(shiyin)
Fk:loadTranslationTable{
  ["ld__n_libai"] = "李白",
  ["#ld__n_libai"] = "",
  ["designer:ld__n_libai"] = "静谦",
  ["illustrator:ld__n_libai"] = "",

  ["ld__n_jiuge"] = "酒歌",
  [":ld__n_jiuge"] = "你可以明置所有手牌，然后视为使用一张【酒】。",
  ["ld__n_jianxing"] = "剑行",
  [":ld__n_jianxing"] = "主将技，你可以将一张明置牌当【杀】使用，然后若此牌造成伤害，你可以暗置任意张明置牌。",
  ["ld__n_shiyin"] = "诗吟",
  [":ld__n_shiyin"] = "副将技，此武将牌上单独的阴阳鱼个数-1；当你成为牌的目标后，若此牌牌名字数与你明置手牌数相等，你可以摸一张牌，然后你暗置一张牌。",

  ["~ld__n_libai"] = "",
}

local lilinfu = General(extension, "ld__n_lilinfu", "tangz", 3)
lilinfu.hidden = true
local nongzhang = fk.CreateTriggerSkill{
  name = "ld__n_nongzhang",
}
local mijian = fk.CreateTriggerSkill{
  name = "ld__n_mijian",
}
lilinfu:addSkill(nongzhang)
lilinfu:addSkill(mijian)
Fk:loadTranslationTable{
  ["ld__n_lilinfu"] = "李林甫",
  ["#ld__n_lilinfu"] = "",
  ["designer:ld__n_lilinfu"] = "教父",
  ["illustrator:ld__n_lilinfu"] = "",

  ["ld__n_nongzhang"] = "弄獐",
  [":ld__n_nongzhang"] = "锁定技，你的【无懈可击】/【闪】视为【闪】/【无懈可击】；其他角色使用的牌对你造成伤害时，若你以此法响应过此牌，你防止此伤害并移除此武将牌。",
  ["ld__n_mijian"] = "蜜剑",
  [":ld__n_mijian"] = "出牌阶段限一次，你可以令一名手牌数小于体力上限的角色将手牌摸至体力上限并展示所有手牌，若其中有黑色牌，你对其造成1点伤害。",

  ["~ld__n_lilinfu"] = "",
}

local weizheng = General(extension, "ld__n_weizheng", "tangz", 3)
weizheng.hidden = true
local zhihua = fk.CreateTriggerSkill{
  name = "ld__n_zhihua",
}
local lianjian = fk.CreateTriggerSkill{
  name = "ld__n_lianjian",
}
local zhengdian = fk.CreateTriggerSkill{
  name = "ld__n_zhengdian",
}
weizheng:addSkill(zhihua)
weizheng:addSkill(lianjian)
weizheng:addSkill(zhengdian)
Fk:loadTranslationTable{
  ["ld__n_weizheng"] = "魏徵",
  ["#ld__n_weizheng"] = "",
  ["designer:ld__n_weizheng"] = "教父",
  ["illustrator:ld__n_weizheng"] = "",

  ["ld__n_zhihua"] = "致化",
  [":ld__n_zhihua"] = "锁定技，其他角色牌指定你为目标时，若此牌花色与你明置牌中唯一最多的花色相同，取消之。",
  ["ld__n_lianjian"] = "连谏",
  [":ld__n_lianjian"] = "当你使用牌指定唯一目标后，若其有此花色的明置手牌，你可令其暗置此花色的所有明置牌并摸一张牌。",
  ["ld__n_zhengdian"] = "政典",
  [":ld__n_zhengdian"] = "当锦囊牌于你的回合内进入弃牌堆时，你可以明置一张花色相同的手牌，改为将此牌置于牌堆顶或牌堆底。",

  ["~ld__n_weizheng"] = "",
}

local yuantiangang = General(extension, "ld__n_yuantiangang", "tangz", 3)
yuantiangang.hidden = true
local xiangshu = fk.CreateTriggerSkill{
  name = "ld__n_xiangshu",
  events = {fk.EventPhaseStart},
}
local mingjie = fk.CreateTriggerSkill{
  name = "ld__n_mingjie",
  events = {fk.EventPhaseStart},
}
yuantiangang:addSkill(xiangshu)
yuantiangang:addSkill(mingjie)
Fk:loadTranslationTable{
  ["ld__n_yuantiangang"] = "袁天罡",
  ["#ld__n_yuantiangang"] = "",
  ["designer:ld__n_yuantiangang"] = "尤提姆",
  ["illustrator:ld__n_yuantiangang"] = "",
  
  ["ld__n_xiangshu"] = "相术",
  [":ld__n_xiangshu"] = "当你于出牌阶段内首次使用牌指定唯一目标后，你可以令其展示所有手牌，若与你使用的此牌花色均不相同且其与你势力相同，你可以令其选择是否变更。",
  ["ld__n_mingjie"] = "命诫",
  [":ld__n_mingjie"] = "结束阶段，你可以将所有手牌分配给任意名其他角色，若如此做，直至你的下个回合开始，当你于每回合首次进入濒死状态时，你摸两张牌。",

  ["~ld__n_yuantiangang"] = ""
}

local zhangsunwuji = General(extension, "ld__n_zhangsunwuji", "tangz", 4)
zhangsunwuji.hidden = true
local xingbian = fk.CreateActiveSkill{
  name = "ld__n_xingbian",
}
local zhongwang = fk.CreateActiveSkill{
  name = "ld__n_zhongwang",
}
zhangsunwuji:addSkill(xingbian)
zhangsunwuji:addSkill(zhongwang)
Fk:loadTranslationTable{
  ["ld__n_zhangsunwuji"] = "长孙无忌",
  ["#ld__n_zhangsunwuji"] = "",
  ["designer:ld__n_zhangsunwuji"] = "小曹神&教父",
  ["illustrator:ld__n_zhangsunwuji"] = "",

  ["ld__n_xingbian"] = "兴变",
  [":ld__n_xingbian"] = "其他角色的出牌阶段开始时，你可以明置至多体力值数张手牌，然后其可以用手牌与你交换其中任意张牌并明置之。",
  ["ld__n_zhongwang"] = "众望",
  [":ld__n_zhongwang"] = "锁定技，若你的明置牌数大于与你势力相同的角色数，与你势力相同的角色使用明置牌可以额外指定一个目标。",

  ["~ld__n_zhangsunwuji"] = "",
}

local taipinggongzhu = General(extension, "ld__n_taipinggongzhu", "tangz", 3, 3, General.Female)
taipinggongzhu.hidden = true
local guichong = fk.CreateTriggerSkill{
  name = "ld__n_guichong",
}
local fuchao = fk.CreateTriggerSkill{
  name = "ld__n_fuchao",
}
taipinggongzhu:addSkill(guichong)
taipinggongzhu:addSkill(fuchao)
Fk:loadTranslationTable{
  ["ld__n_taipinggongzhu"] = "太平公主",
  ["#ld__n_taipinggongzhu"] = "",
  ["designer:ld__n_taipinggongzhu"] = "静谦",
  ["illustrator:ld__n_taipinggongzhu"] = "",

  ["ld__n_guichong"] = "贵宠",
  [":ld__n_guichong"] = "当你回复体力后，你可以明置一张手牌，然后将场上的一张装备牌移动至你的装备区。",
  ["ld__n_fuchao"] = "覆朝",
  [":ld__n_fuchao"] = "其他角色的结束阶段，你可以将一张装备牌置入其装备区，然后对其发起“军令”：若其不执行，你获得其一张手牌并明置之；若你的手牌均明置，你以此法发起的“军令”强制执行。",

  ["~ld__n_taipinggongzhu"] = "",
}

local lordlishimin = General(extension, "ld__n_lordlishimin", "tangz", 4)
lordlishimin.hidden = true
H.lordGenerals["hs__n_lishimin"] = "ld__n_lordlishimin"
local zhenguan = fk.CreateTriggerSkill{
  name = "ld__n_zhenguan",
  anim_type = "support",
  frequency = Skill.Compulsory,
  on_acquire = function (self, player, is_start)
    local room = player.room
    player:broadcastSkillInvoke(self.name)
    room:notifySkillInvoked(player, self.name)
    room:handleAddLoseSkills(player, "#ld__n_lingyange")
  end,
  on_lose = function (self, player, is_death)
    local room = player.room
    room:handleAddLoseSkills(player, "-#ld__n_lingyange")
  end,
}
local lingyange = fk.CreateTriggerSkill{
  name = "#ld__n_lingyange",
  anim_type = "support",
  events = {fk.EventPhaseStart},
  can_trigger = function (self, event, target, player, data)
    if target ~= player then return end
    if target.phase == Player.Play then
      return not target:isKongcheng() and table.find(player.room.alive_players, function (p)
        return H.compareKingdomWith(p, target) and p:hasShownSkill(self)
      end)
    elseif target.phase == Player.Start then
      return #H.GetShownCards(target) > 0 and table.find(player.room.alive_players, function (p)
        return H.compareKingdomWith(p, target) and p:hasShownSkill(self)
      end)
    end
  end,
  on_cost = function (self, event, target, player, data)
    if player.phase == Player.Play then
      return player.room:askForSkillInvoke(target, self.name)
    else
      local showns = H.GetShownCards(player)
      local pat = ".|.|.|.|.|.|"..table.concat(showns, ",")
      local cards = player.room:askForCard(player, 1, 998, false, self.name, true, pat, "#ld__n_lingyange-dis")
      if #cards > 0 then
        self.cost_data = cards
        return true
    end
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    if player.phase == Player.Play then
      local showns = H.GetShownCards(target)
      local notshowns = table.filter(player:getCardIds("h"), function (id)
        return Fk:getCardById(id):getMark(MarkEnum.ShownCards) == 0 end)
      local flag = {card_data = {{"ld__n_lingyange-showns", showns}, {"ld__n_lingyange-notshowns", notshowns}}}
      local cards = room:askForCardsChosen(target, target, 0, player.hp, flag, self.name, "#ld__n_lingyange-change:::"..player.hp)
      local toshow = table.filter(cards, function(id) return table.contains(notshowns, id) end)
      local tohide = table.filter(cards, function(id) return table.contains(showns, id) end)
      if #tohide > 0 then
        H.HideCards(target, tohide)
      end
      toshow = U.moveCardsHoldingAreaCheck(room, toshow, room.logic:getCurrentEvent().id)
      if #toshow > 0 then
        H.ShowCards(target, toshow)
      end
    else
      local n = #self.cost_data
      room:throwCard(self.cost_data, self.name, player)
      if player.dead then return end
      local specialmechanism = {
        --基本概念
        "明置", "暗置",  -- "主将", "副将", "鏖战", "国战标记", "阴阳鱼", "珠联璧合", "先驱", "野心家", --"未确定势力", "其他势力", "没有势力", "叠置"
        --君临天下
        "阵法", "队列", "围攻", "移除", "变更", "军令", "纵横", --"君主", "召唤", "合纵", "大势力", "小势力", "调离",
        --日月
        "推举", "战策", "借调",
      }
      local allgenerals = table.simpleClone(room.general_pile)
      table.shuffle(allgenerals)
      local generals, skills = {}, {}
      for _, general_name in ipairs(allgenerals) do
        local general = Fk.generals[general_name]
        local gskills = general:getSkillNameList()
        local gskillsfilter = {}
        for _, s in ipairs(gskills) do
          local text = Fk:translate(":"..s, "zh_CN")
          for _, str in ipairs(specialmechanism) do
            if string.find(text, str) then
              table.insertIfNeed(gskillsfilter, s)
              break
            end
          end
        end
        if #gskillsfilter > 0 and not table.contains(generals, general_name) then
          table.insert(generals, general_name)
          table.insert(skills, gskillsfilter)
        end
        if #generals >= n then
          break
        end
      end
      if #generals > 0 then
        local choices
        local result = player.room:askForCustomDialog(player, self.name,
        "packages/new_heg_my/qml/ChooseGeneralSkillsBox.qml", {
          generals, skills, 1, 1, "#ld__n_lingyange-choice", false
        })
        if result ~= "" then
          choices = json.decode(result)
        end
        room:addTableMark(player, "@ld__n_lingyange-turn", choices[1])
        room:handleAddLoseSkills(player, choices, nil)
        room.logic:getCurrentEvent():findParent(GameEvent.Turn):addCleaner(function()
          room:handleAddLoseSkills(player, "-"..choices[1], nil)
        end)
      end
    end
  end,

  refresh_events = {"fk.AfterCardsShown", fk.AfterCardsMove, fk.GameOverJudge},
  can_refresh = function (self, event, target, player, data)
    return player:hasShownSkill(self) and table.every(player.room.alive_players, function (p)
      return #H.GetShownCards(p) == p:getHandcardNum()
    end)
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(self.name)
    room:notifySkillInvoked(player, self.name, "big")
    for _, p in ipairs(room:getAllPlayers()) do --先把野人亮出来，防止拉拢
      if p:getMark("__heg_wild") == 1 and p.general == "anjiang" then
        p:revealGeneral(false)
      end
    end
    for _, p in ipairs(room:getAllPlayers()) do
      if p.general == "anjiang" then p:revealGeneral(false) end
      if p.deputyGeneral == "anjiang" then p:revealGeneral(true) end
    end
    local targets = table.filter(room.players, function (p) return H.compareKingdomWith(p, player) end)
    room:doIndicate(player.id, table.map(targets, Util.IdMapper))
    room:gameOver(player.role)
  end,
}

local yonggu = fk.CreateTriggerSkill{
  name = "ld__n_yonggu",
}
local wanning = fk.CreateTriggerSkill{
  name = "ld__n_wanning",
  frequency = Skill.Compulsory,
  events = {fk.AfterCardTargetDeclared},
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self) and data.card.name == "ld__n_guotaiminan" and (player == target or not H.compareKingdomWith(player, target))
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    data.tos = {}
    local targets = table.filter(room:getAlivePlayers(false), function (p) return H.compareKingdomWith(p, player) and not player:isProhibited(p, data.card) end)
    if player ~= target then
      targets = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1, "#ld__n_wanning-choose", self.name, false)
      targets = table.map(targets, Util.Id2PlayerMapper)
    else
      room:doIndicate(player.id, table.map(targets, Util.IdMapper))
    end
    for i = 1, #targets, 1 do
      TargetGroup:pushTargets(data.tos, targets[i].id)
    end
  end,
}
Fk:addSkill(lingyange)
lordlishimin:addSkill(zhenguan)
lordlishimin:addSkill(yonggu)
lordlishimin:addSkill(wanning)
Fk:loadTranslationTable{
  ["ld__n_lordlishimin"] = "君李世民",
  ["#ld__n_lordlishimin"] = "", 
  ["designer:ld__n_lordlishimin"] = "教父",
  ["illustrator:ld__lordlishimin"] = "",

  ["ld__n_zhenguan"] = "贞观",
  [":ld__n_zhenguan"] = "<b><font color='goldenrod'>君主技</font></b>，你拥有“天策凌烟阁”。<br>" ..
  "#<b>天策凌烟阁</b>：①唐势力角色的出牌阶段开始时，其可以调整至多体力值数张手牌的明暗置状态。<br>" ..
  "②唐势力角色准备阶段，其可以弃置任意张已明置手牌，检索等量拥有国战特殊机制的武将牌并获得其中一张武将牌上一个带有此对应机制的技能直至回合结束。<br>"..
  "③若所有角色的所有手牌均明置，唐势力获胜。",
  ["ld__n_wanning"] = "万宁",
  [":ld__n_wanning"] = "锁定技，非唐势力角色使用【国泰民安】的目标改为你选择的一名唐势力角色；你使用【国泰民安】的目标改为所有唐势力角色。<br> 【国泰民安】：出牌阶段，对一名与你势力相同的角色使用，目标角色回复1点体力并摸一张牌，若此牌未生效，你叠置。",
  ["ld__n_yonggu"] = "咏古",
  [":ld__n_yonggu"] = "出牌阶段限一次，",

  ["#ld__n_lingyange"] = "凌烟阁",
  ["ld__n_lingyange-showns"] = "明置牌",
  ["ld__n_lingyange-notshowns"] = "暗置牌",
  ["#ld__n_lingyange-change"] = "天策凌烟阁：你可调整至多%arg张手牌的明暗置状态",
  ["#ld__n_lingyange-dis"] = "天策凌烟阁：你可以弃置任意张明置牌，检索等量拥有国战特殊机制的武将牌",
  ["#ld__n_lingyange-choice"] = "天策凌烟阁：选择一个技能获得",
  ["@ld__n_lingyange-turn"] = "凌烟阁",
  ["#ld__n_wanning-choose"] = "万宁：选择【国泰民安】的目标",
  
  --["ld__n_guotaiminan"] = "【国泰民安】：出牌阶段，对一名与你势力相同的角色使用，目标角色回复1点体力并摸一张牌，若此牌未生效，你叠置。", -- 替换 方块A 挟天子以令诸侯

  ["~ld__n_lordlishimin"] = "",
}


local extension_card = Package("tangz_ld_cards", Package.CardPack)
extension_card.extensionName = "new_heg_my"
extension_card.game_modes_whitelist = { 'mul_heg_mode' }

local guotaiminanSkill = fk.CreateActiveSkill{
  name = "ld__n_guotaiminan_skill",
  prompt = "#ld__n_guotaiminan_skill",
  target_num = 1,
  mod_target_filter = function(self, to_select, selected, player)
    local target = Fk:currentRoom():getPlayerById(to_select)
    return H.compareKingdomWith(target, player)
  end,
  target_filter = Util.TargetFilter,
  can_use = Util.CanUse,
  on_effect = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.to)
    local e = player.room.logic:getCurrentEvent()
    while e do
      if e.event == GameEvent.UseCard then
        local use = e.data[1]
        if use.card.name == "ld__n_guotaiminan" then
          use.extra_data = use.extra_data or {}
          use.extra_data.ld__n_guotaiminaneffect = true
          break
        end
      end
      e = e.parent
    end
    if target.dead then return end
    room:recover{
      who = target,
      recoverBy = player,
      card = effect.card,
      num = 1,
      skillName = self.name
    }
    if target.dead then return end
    target:drawCards(1, "ld__n_guotaiminan")
  end,
  on_action = function (self, room, use, finished)
    if finished then
      local player = room:getPlayerById(use.from)
      if not player.dead and not (use.extra_data and use.extra_data.ld__n_guotaiminaneffect) then
        player:turnOver()
      end
    end
  end
}
local guotaiminan = fk.CreateTrickCard{
  name = "ld__n_guotaiminan",
  skill = guotaiminanSkill,
  suit = Card.Diamond,
  number = 1,
}
--卡牌替换，耦合的，原方法只能替换某牌名的全部牌，见nos_new_heg.lua
extension_card:addCard(guotaiminan)
Fk:loadTranslationTable{
  ["tangz_ld_cards"] = "鼎业积功卡牌",
  ["ld__n_guotaiminan"] = "国泰民安",
  ["ld__n_guotaiminan_skill"] = "国泰民安",
  [":ld__n_guotaiminan"] = "锦囊牌<br/><b>时机</b>：出牌阶段<br/><b>目标</b>：一名与你势力相同的角色<br/><b>效果</b>：目标角色回复1点体力并摸一张牌，若此牌未生效，你叠置。",
  ["#ld__n_guotaiminan_skill"] = "选择势力与你相同的一名角色，其回复1点体力并摸一张牌，若此牌未生效，你叠置",
}

return {extension, extension_card}
